/***************************************************************************
创建者：华磊
 开始时间：               : 2020.1１.２４
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   io窗口程序
*                                                                         *
 ***************************************************************************/
#include "distatus.h"
#include "ui_distatus.h"
#include "systemstatus.h"
#include <QMessageBox>

DiStatus::DiStatus(int styleId,QWidget *parent) :
    QWidget(parent),
  ui(new Ui::DiStatus)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    setTableView();

    QString imagepath ="/agvwork/AgvHmi/baseBag/hmiProject/resources/systemstatusresources/on.png";
    QString imagepath2 ="/agvwork/AgvHmi/baseBag/hmiProject/resources/systemstatusresources/off.png";
    QPixmap image(imagepath);
    QPixmap image2(imagepath2);

    icon = QIcon(image);
    icon2 = QIcon(image2);

    initFlag = true;

}

void DiStatus::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->tableView->setGeometry(10,35,440,350);
        ui->textEdit->setGeometry(10,400,300,40);
        ui->pushButton_2->setGeometry(370,400,76,41);
        break;
    default:
        break;
    }
}

void DiStatus::on_pushButton_clicked()
{
    int t =ui->lineEdit->text().toInt();
    ui->tableView->selectRow(t-1);
}

void DiStatus::showDiStatusView()
{
    emit diStatusSearch();
}

void DiStatus::changeLanguage()
{
    ui->retranslateUi(this);
    setTableView();
}

void DiStatus::setTableView()
{
    model = new QStandardItemModel();
    ui->tableView->setModel(model);
    model->setColumnCount(3);

    headerView = ui->tableView->horizontalHeader();
//    headerView->setResizeMode(QHeaderView::Fixed);

    ui->tableView->setColumnWidth(0,100);
    ui->tableView->setColumnWidth(1,100);
    ui->tableView->setColumnWidth(2,240);
    ui->tableView->verticalHeader()->hide();
    ui->tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    model->setHeaderData(0,Qt::Horizontal,tr("序号"));
    model->setHeaderData(1,Qt::Horizontal,tr("值"));
    model->setHeaderData(2,Qt::Horizontal,tr("说明"));

    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式，选择单行
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void DiStatus::updateView()
{
    int n = diValue.size();
    if(initFlag)
    {
        model->setRowCount(n);
    }
    bool viewChange = false;
    for(int i=0 ;i<n ;i++)
    {
        if(diValue[i])
        {
            if(initFlag)
            {
                model->setItem(i,0,new QStandardItem( QString::number(i+1) ));
                model->setItem(i,1,new QStandardItem(icon,""));

                if(note.size() == n)
                {
                    model->setItem(i,2,new QStandardItem(note[i]));
                }

                model->item(i,0)->setTextAlignment(Qt::AlignCenter);

                tmpDiValue[i] = diValue[i];

                viewChange = true;
            }
            else
            {
                if(tmpDiValue[i] != diValue[i])
                {
                    model->item(i,1)->setIcon(icon);
                    tmpDiValue[i] = diValue[i];

                    viewChange = true;
                }
            }
        }
        else if(!diValue[i])
        {
            if(initFlag)
            {
                model->setItem(i,0,new QStandardItem( QString::number(i+1) ));
                model->setItem(i,1,new QStandardItem(icon2,""));

                if(note.size() == n)
                {
                    model->setItem(i,2,new QStandardItem(note[i]));
                }

                model->item(i,0)->setTextAlignment(Qt::AlignCenter);

                tmpDiValue[i] = diValue[i];

                viewChange = true;
            }
            else
            {
                if(tmpDiValue[i] != diValue[i])
                {
                    model->item(i,1)->setIcon(icon2);
                    tmpDiValue[i] = diValue[i];

                    viewChange = true;
                }
            }
        }
        else
        {

        }
        //ui->tableView->resizeRowsToContents();      //行高根据内容自适应
    }
    initFlag = false;//初始化完成

    if(viewChange)
    {
        if(n>11)
        {
            ui->tableView->setColumnWidth(0,90);
            ui->tableView->setColumnWidth(1,90);
            ui->tableView->setColumnWidth(2,230);
        }
        else
        {
            ui->tableView->setColumnWidth(0,100);
            ui->tableView->setColumnWidth(1,100);
            ui->tableView->setColumnWidth(2,240);
        }
        viewChange = false;
    }
}

void DiStatus::on_pushButton_2_clicked()
{
    if(ui->tableView->currentIndex().row()>=0)
    {

        if(ui->textEdit->toPlainText().length()==0)
        {
            QMessageBox::information(this,tr("Error"),QStringLiteral("添加注释不能为空"),QMessageBox::Ok);
        }
        else
        {
            int i=ui->tableView->currentIndex().row();
            //note.push_back(ui->textEdit->toPlainText());
            // 这里的选中行从1开始,控件返回行数从0开始
            emit setNote(i+1,ui->textEdit->toPlainText());

            //update view
            note[i] = ui->textEdit->toPlainText();
            model->item(i,2)->setText(note[i]);
        }
    }
}
